package com.outsourcing.bottle.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.BottleTypeTable;
import com.outsourcing.bottle.domain.BottleFriendEntry;
import com.outsourcing.bottle.domain.BottleTypeEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.BottleDetailActivity;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.ui.fragment.FriendFragment;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.TextUtil;

public class BTFriendListAdapter extends BaseAdapter {

	private Context context = null;
	private List<BottleFriendEntry> mbtfriendList = null;
	private BottleTypeEntry entry = null;
	private BottleTypeTable btt = null;
	private FriendFragment fragment;
	private ImageLoader btConfigLoader;
	
	public BTFriendListAdapter(Context context,FriendFragment fragment,
			List<BottleFriendEntry> mbtfriendList) {
		this.context = context;
		this.fragment = fragment;
		this.mbtfriendList = mbtfriendList;
		btConfigLoader = new ImageLoader(context, AppContext.BOTTLE_CONFIG);
		btt = new BottleTypeTable();
	}

	@Override
	public int getCount() {
		return mbtfriendList==null?0:mbtfriendList.size();
	}

	@Override
	public Object getItem(int position) {
		return mbtfriendList==null?null:mbtfriendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BTFriendViewHolder holder = null;
		if (convertView == null
				|| ((BTFriendViewHolder) convertView.getTag()).flag != position) {
			holder = new BTFriendViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.bottle_list_avatar_item, null);
			holder.flag = position;
			holder.mAuthorView = (RemoteImageView) convertView
					.findViewById(R.id.iv_author_photo);
			holder.mBtType = (ImageView)convertView.findViewById(R.id.iv_bottle_type);
			// content layout
			LinearLayout contentLayout = (LinearLayout) convertView
					.findViewById(R.id.feed_post_body);
			View view = LayoutInflater.from(context).inflate(
					R.layout.friend_list_detail_item, null);
			holder.mNickName = (TextView) view.findViewById(R.id.tv_nickname);
			holder.mSex = (ImageView)view.findViewById(R.id.bt_sex);
			holder.mDoing = (TextView) view.findViewById(R.id.tv_doing);
			holder.mLocation = (TextView) view.findViewById(R.id.tv_location);
			holder.mTag = (TextView) view.findViewById(R.id.tv_tag);
			holder.mDistance = (TextView)view.findViewById(R.id.tv_distance);
			holder.mFriendLocation = (LinearLayout)view.findViewById(R.id.ll_friend_location);
			holder.mCommentLocation = (TextView) view
					.findViewById(R.id.tv_comment_location);
			holder.mbtExPhoto = (Button) view
					.findViewById(R.id.bt_exchange_photo);
			holder.mbtExProfile = (Button) view
					.findViewById(R.id.bt_exchange_profile);

			contentLayout.addView(view);

						convertView.setTag(holder);
		} else {
			holder = (BTFriendViewHolder) convertView.getTag();
		}
		final BottleFriendEntry btFriendEntry = (BottleFriendEntry) getItem(position);

		try {
			holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_big);
			holder.mAuthorView.setImageUrl(btFriendEntry.getBtf_avatar());
		} catch (Exception e) {
			e.printStackTrace();
			holder.mAuthorView
					.setImageResource(R.drawable.avatar_default_big);
		}
		holder.mAuthorView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
				spaceIntent.putExtra("ouid", btFriendEntry.getBtf_uid());
				context.startActivity(spaceIntent);
			}
		});
		
		try {
			entry = btt.getBottleType(btFriendEntry.getBttypeid());
			btConfigLoader.DisplayImage(entry.getBttype_sjwicon(), holder.mBtType, R.drawable.bottle_type_1);
		} catch (Exception e) {
			e.printStackTrace();
			holder.mBtType.setImageResource(R.drawable.bottle_type_1);
		}
		holder.mBtType.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				fragment.doSendSingleBtPri(btFriendEntry.getBttypeid(),
						btFriendEntry.getBtid());
			}
		});
		
		String nickName = "<font color='#3F99D8'>"+ btFriendEntry.getBtf_nickname()+ "</font>";
//		String sex = (((btFriendEntry.getBtf_sex())==1)?("("+TextUtil.R("login_txt_register_male")+")"):("("+TextUtil.R("login_txt_register_female")+")"));
		holder.mNickName.setText(Html.fromHtml(nickName));
		int sex_res = btFriendEntry.getBtf_sex() == 1 ? R.drawable.male:R.drawable.female;
		holder.mSex.setBackgroundResource(sex_res);
		if (!TextUtils.isEmpty(btFriendEntry.getBtf_doing())) {
			holder.mDoing.setVisibility(View.VISIBLE);
			String doingTips ="<font color='#000000'>" +TextUtil.R("friend_doing_tips") + "</font>";
			holder.mDoing.setText(Html.fromHtml(doingTips+btFriendEntry.getBtf_doing()));	
		}else {
			holder.mDoing.setVisibility(View.GONE);
		}
		if ((!TextUtils.isEmpty(btFriendEntry.getBtf_country()))||(!TextUtils.isEmpty(btFriendEntry.getBtf_city()))) {
			holder.mLocation.setVisibility(View.VISIBLE);
			String fromTips =TextUtil.R("friend_comfrom_tips");
			holder.mLocation.setText(fromTips+btFriendEntry.getBtf_city()+" "+btFriendEntry.getBtf_country());	
		}else {
			holder.mLocation.setVisibility(View.GONE);
		}
		
		String feedNickName = "<font color='#3F99D8'>"+ btFriendEntry.getFeed_nickname()+ "</font>";
		String feedTime = (null!=btFriendEntry.getFeed_time()&&btFriendEntry.getFeed_time().length()>0)? ("<font color='#000000'>"+" #"+btFriendEntry.getFeed_time()+ "</font>"):"";
			holder.mTag.setText(Html.fromHtml(feedNickName+":"+btFriendEntry.getFeed_content()+feedTime));
		if (!TextUtils.isEmpty(btFriendEntry.getFeed_location())) {
			holder.mFriendLocation.setVisibility(View.VISIBLE);
			holder.mCommentLocation.setText(btFriendEntry.getFeed_location());
			holder.mCommentLocation.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent bottleDetailIntent = new Intent(context,
							BottleDetailActivity.class);
					bottleDetailIntent.putExtra("btid", btFriendEntry.getBtid());
					bottleDetailIntent.putExtra("type", 1);// 1，LikeList2,location
					context.startActivity(bottleDetailIntent);
				}
			});
			String distance = (null!=btFriendEntry.getFeed_distance()&&btFriendEntry.getFeed_distance().length()>0)? ("<font color='#000000'>"+btFriendEntry.getFeed_distance()+ "</font>"):"";
			holder.mDistance.setText(Html.fromHtml(TextUtil.R("friend_distance")+" "+distance));
		}else {
			holder.mFriendLocation.setVisibility(View.GONE);
		}
		
		if (btFriendEntry.getBtf_isfriend()==1) {
			holder.mbtExProfile.setText(TextUtil.R("friend_whose_profile"));
			holder.mbtExPhoto.setText(TextUtil.R("friend_whose_photo"));
			holder.mbtExProfile.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,UserSpaceActivity.class);
					intent.putExtra("ouid", btFriendEntry.getBtf_uid());
					context.startActivity(intent);
				}
			});
			holder.mbtExPhoto.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fragment.doVisitAlbum(btFriendEntry.getBtf_uid());
				}
			});
		}else{
			holder.mbtExProfile.setText(TextUtil.R("friend_exchange_profile"));
			holder.mbtExPhoto.setText(TextUtil.R("friend_exchange_photo"));
			holder.mbtExProfile.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fragment.doExchange(AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE,btFriendEntry.getBtf_uid());
				}
			});
			holder.mbtExPhoto.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fragment.doExchange(AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE,btFriendEntry.getBtf_uid());
				}
			});
		}


		return convertView;
	}

	
	public static class BTFriendViewHolder {
		public ImageView mSex;
		RemoteImageView mAuthorView;
		ImageView mBtType;
		ImageView mExType;

		TextView mNickName;
		TextView mDoing;
		TextView mLocation;
		TextView mTag;
		TextView mCommentLocation;
		TextView mDistance;
		Button mbtExPhoto;
		Button mbtExProfile;
		LinearLayout mFriendLocation;

		int flag;
	}

}
